var app = getApp()
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    inputShowed: false,
    inputVal: "",

    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },
    startNum: 1, //页数

    total: '0',

    search: false, //判断模糊查询方法


  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      search: false
    });
    this.onLoad();
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },

  searInput: function (e) {
    var _that = this
    console.log(_that.data.inputVal)
    wx.showLoading({
      title: '搜索中',
    })
    if (_that.data.inputVal.length != '') {
      wx.request({
        url: api.DeviceManageList + '?current=1&size=10',
        data: {
          deviceName: _that.data.inputVal,
          current: "1", //请求第一页
          size: "10"
        },
        method: 'POST',
        header: {
          'Accept': 'application/json',
          'ACCESS-TOKEN': wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          if (res.data.code == '1') {
            var listSize = res.data.obj.records.length;
            console.log('listSize=' + listSize);
            var newData = _that.data.myAgentList.list;
            newData = res.data.obj.records;
            var total1 = res.data.obj.total;
            var newList = {};
            //当长度小于10表示没有下页数据了
            if (listSize < 10) {
              newList.hasMore = false;
            } else {
              newList.hasMore = true;
            }
            newList.list = newData;
            console.log('newData=' + newData);
            newList.listSize = listSize;
            _that.setData({
              myAgentList: newList
            });
            console.log('myAgentList=' + _that.data.myAgentList);
            _that.setData({
              startNum: 2,
              total: total1,
              search: true
            });

            console.log("newList.list=" + newList.list)

            if (newList.list.length == 0) {
              wx.showModal({
                title: '提示',
                content: "抱歉！你搜索的设备不存在！",
                confirmColor: "#009FD1",
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    _that.hideInput()
                  }
                }
              });
            }



          } else {
            // util.msg("错误", res.data.msg)
            console.log("网络通，数据不成功")
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res.data)
          // util.msg("错误", "通讯失败")
        }
      })

    } else {
      wx.showToast({
        title: "输入内容不能为空",
        icon: 'none',
        duration: 2000
      });
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //加载页面
    var _that = this
    var _url = api.DeviceManageList + '?current=1&size=5';
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: _url,
      data: {
        current: "1", //请求第一页
        size: "5"
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var listSize = res.data.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          newData = res.data.obj.records;
          var total1 = res.data.obj.total;
          var newList = {};
          //当长度小于10表示没有下页数据了
          if (listSize < 5) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          console.log('newData=' + newData);
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList,
            total: total1
          });

          console.log('myAgentList=' + _that.data.myAgentList);

          _that.setData({
            startNum: 2
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })
  },

  hotActivityLoadMore: function (e) {
    var _that = this;
    if (_that.data.search == false) {
      //判断是否还有下一页
      if (_that.data.myAgentList.hasMore == true) {
        wx.showLoading({
          title: '加载中',
        })
        var pageNum = _that.data.startNum
        var _url = api.DeviceManageList + '?current=' + pageNum + '&size=5';
        wx.request({
          url: _url,
          data: {
            current: pageNum, //第N页
            size: "5"
          },
          method: 'POST',
          header: {
            'Accept': 'application/json',
            'ACCESS-TOKEN': wx.getStorageSync('token')
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.code == "1") {
              console.log("网络通，数据成功")
              wx.hideLoading();
              var listSize = res.data.obj.records.length;
              var newData = _that.data.myAgentList.list;
              //concat是在原有基础上进行增加
              newData = newData.concat(res.data.obj.records);
              var newList = {};
              //当长度等于于0表示当前没有数据，停止
              if (listSize == 0) {
                newList.hasMore = false;
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                return;
                //当长度小于10表示没有下页数据了
              } else if (listSize < 5) {
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                newList.hasMore = false;
              } else {
                newList.hasMore = true;
              }
              newList.list = newData;
              newList.listSize = listSize;
              _that.setData({
                myAgentList: newList
              });

              _that.setData({
                startNum: pageNum + 1 //页数加1
              })
            } else {
              // util.msg("错误", res.data.msg)
              console.log("网络通，数据不成功")
            }

          },
          fail: function (res) {
            console.log(res.data)
            // util.msg("错误", "通讯失败")
            console.log("失败")
          }
        })
      } else {
        wx.showToast({
          title: "已加载全部列表",
          icon: 'none',
          duration: 2000
        });
      }
    } else if (_that.data.search == true) {
      //判断是否还有下一页
      if (_that.data.myAgentList.hasMore == true) {
        wx.showLoading({
          title: '加载中',
        })
        var pageNum = _that.data.startNum
        var _url = api.DeviceManageList + '?current=' + pageNum + '&size=10';
        wx.request({
          url: _url,
          data: {
            deviceName: _that.data.inputVal,
            current: pageNum, //第N页
            size: "10"
          },
          method: 'POST',
          header: {
            'Accept': 'application/json',
            'ACCESS-TOKEN': wx.getStorageSync('token')
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.code == "1") {
              console.log("网络通，数据成功")
              wx.hideLoading();
              var listSize = res.data.obj.records.length;
              var newData = _that.data.myAgentList.list;
              //concat是在原有基础上进行增加
              newData = newData.concat(res.data.obj.records);
              var newList = {};
              //当长度等于于0表示当前没有数据，停止
              if (listSize == 0) {
                newList.hasMore = false;
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                return;
                //当长度小于10表示没有下页数据了
              } else if (listSize < 10) {
                wx.showToast({
                  title: "已加载全部列表",
                  icon: 'none',
                  duration: 2000
                });
                newList.hasMore = false;
              } else {
                newList.hasMore = true;
              }
              newList.list = newData;
              newList.listSize = listSize;
              _that.setData({
                myAgentList: newList
              });

              _that.setData({
                startNum: pageNum + 1 //页数加1
              })
            } else {
              // util.msg("错误", res.data.msg)
              console.log("网络通，数据不成功")
            }

          },
          fail: function (res) {
            console.log(res.data)
            // util.msg("错误", "通讯失败")
            console.log("失败")
          }
        })
      } else {
        wx.showToast({
          title: "已加载全部列表",
          icon: 'none',
          duration: 2000
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})